﻿using Architecture.API.Application.TraceabilityCommand.ShoppingNameCommand;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Traceability.Domain;

namespace Architecture.API.Controllers
{
    /// <summary>
    /// API控制器--商品名称仓储
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ShoppingNameManagementController : ControllerBase
    {
        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">命令总线</param>
        public ShoppingNameManagementController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加商品名称信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回受影响的行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddShoppingName(ShoppingNameCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 获取商品名称列表
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回商品名称列表</returns>
        [HttpPost]
        public async Task<ApiResult<List<ShoppingName>>> GetShoppingNameList(ShoppingNameSeachCommand command)

        {
            ApiResult<List<ShoppingName>> result = new ApiResult<List<ShoppingName>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
